from os import error
from flask import Blueprint,render_template, request,session,redirect
from config import conn,cur,mutex ,r as redis
import datetime

Doctor = Blueprint('Doctor',__name__)
 
@Doctor.route('/',methods=["GET", "POST"])
def index():
    id = session.get('id') 
    name = session.get('name')

    sqlstr='select p.id, p.name, p.gender, p.age from personal_info p where p.doctor_id = \'' + str(id)+'\''
    print(sqlstr)
    cur.execute(sqlstr)
    result = cur.fetchall()
    
    return render_template('/doctor/index.html',id =id,name=name,data=result)

@Doctor.route('/instruction',methods=["GET", "POST"])
def instruction():
    id = session.get('id') 
    name = session.get('name')
    pid = request.form.get("id")

    sqlstr='select id, name, time from medication where patient_id = \'' + str(pid)+'\''
    print(sqlstr)
    cur.execute(sqlstr)
    result = cur.fetchall()

    sqlstr='select email, cellphone from personal_info where id = \'' + str(pid)+'\''
    print(sqlstr)
    cur.execute(sqlstr)
    comm = cur.fetchall()
    
    return {"message":"success!", "data":result, "comm":comm}

@Doctor.route('/chat',methods=["GET", "POST"])
def docchat():
    id = session.get('id') 
    name = session.get('name')

    sqlstr='select id, name, email, cellphone from personal_info p where doctor_id = \'' + str(id)+'\''
    print(sqlstr)
    cur.execute(sqlstr)
    result = cur.fetchall()

    sqlstr='select id, email, cellphone from doctor_info where id = \'' + str(id)+'\''
    print(sqlstr)
    cur.execute(sqlstr)
    docinfo = cur.fetchall()
    
    return render_template('/doctor/chat.html',id =id,name=name,data=result,docinfo=docinfo)

@Doctor.route('/modifycom',methods=["GET", "POST"])
def modifycom():
    id = request.form.get("id")
    email = request.form.get("email")
    tel = request.form.get("tel")
    sqlstr='UPDATE doctor_info SET email=\''+email+'\', cellphone=\''+ tel +'\' WHERE id=\''+ id+'\''
    print(sqlstr)
    cur.execute(sqlstr)
    conn.commit()

    return {"message":"success!"}


@Doctor.route('/deletem',methods=["GET", "POST"])
def deletem():
    id = session.get('id') 
    name = session.get('name')
    mid = request.form.get("id")

    sqlstr='delete from medication where id = \'' + str(mid)+'\''
    print(sqlstr)
    cur.execute(sqlstr)
    conn.commit()

    return {"message":"success!"}

@Doctor.route('/modifym',methods=["GET", "POST"])
def modifym():
    id = request.form.get("id")
    name = request.form.get("name")
    time = request.form.get("time")
    sqlstr='UPDATE medication SET name=\''+name+'\', time=\''+ time +'\' WHERE id=\''+ id+'\''
    print(sqlstr)
    cur.execute(sqlstr)
    conn.commit()

    return {"message":"success!"}

@Doctor.route('/addm',methods=["GET", "POST"])
def addm():
    id = session.get('id') 
    name = session.get('name')
    mid = request.form.get("name")
    pid = request.form.get("id")
    time = request.form.get("time")
    print("im in ")

    sqlstr= "insert into medication (\"patient_id\", \"name\", \"time\") values ('"+str(pid)+"','"+mid+"', '"+time+"')"
    print(sqlstr)
    cur.execute(sqlstr)
    conn.commit()

    return {"message":"success!"}

@Doctor.route('/chathis',methods=["GET", "POST"])
def chathis():
    id = session.get('id') 
    name = session.get('name')
    did = request.form.get("did")
    pid = request.form.get("pid")

    sqlstr= "select [text], send_by from [message] where doctor_id='"+did+"' and patient_id='"+pid+"' order by [time]"
    print(sqlstr)
    cur.execute(sqlstr)
    result = cur.fetchall()
    
    return {"message":"success!", "his":result}

@Doctor.route('/sendmss',methods=["GET", "POST"])
def sendmss():
    id = session.get('id') 
    name = session.get('name')
    did = request.form.get("did")
    pid = request.form.get("pid")
    mes = request.form.get("mes")

    sqlstr= "insert into [message] (doctor_id, patient_id, [text], send_by) VALUES ('"+did+"', '"+pid+"', '"+mes+"', 'd');"
    print(sqlstr)
    cur.execute(sqlstr)
    conn.commit()
    
    return {"message":"success!"}

@Doctor.route('/logout',methods=["GET", "POST"])
def logout():
    session.clear()
    return {
        'message':"success!"
    }

# 因为关于医生的路由比较少，所以，缪啊，就把路由放在这里就行，或者自己看着办



@Doctor.before_request
def before():
    if session.get('id') is None or session.get('role') != 'doctor' :
        return redirect('/Login/',code=301)
    else:
        pass